module.exports = {
  //YYYY
  year(dateString) { //yyyy-MM-dd hh:mm:ss
    //console.log("date.year " + dateString);
    if (dateString && dateString !== '') {
      var arr1 = dateString.split(" ");
      var sdate = arr1[0].split('-');
      var date = new Date(sdate[0], sdate[1]-1, sdate[2]);
      return date.getFullYear();
    }
  },
  age(birthday) {
    //console.log("date.age " + birthday);
    if (birthday && birthday !== '') {
      var birthYear = this.year(birthday);
      // console.log(birthYear);
      return new Date().getFullYear() - birthYear;
    }
  },
  //返回与当前的时间差，如果60分钟内，返回分钟
  delta(dateString) {
    if (dateString && dateString !== '') {
      var date = new Date(dateString.replace(/-/g,"/"));
      var current = new Date();
      var minutes = parseInt((current - date) / (1000 * 60));
      var hours = parseInt(minutes / 60);
      var days = parseInt(hours / 24);
      var months = parseInt(days / 30);
      var years = parseInt(months / 12);
      //console.log(years, months, days, hours, minutes);
      // if (years > 0) {
      //   return years + '年前';
      // }
      if (years > 0 || months > 0) {
        // return months + '月前';

        //超过3天
        var newDate= new Date(date);
        // console.log('newDate',newDate);
        var month = newDate.getMonth()+1;
        var day = newDate.getDate();
        if(month<10){
          month = '0'+month;
        }
        if(day<10){
          day = '0'+day;
        }
        return newDate.getFullYear()+"-"+month+"-"+day;
      }
      if (days > 0) {
        return days + '天前';
      }
      if (hours > 0) {
        return hours + '小时前';
      }
      if (minutes === 0) {
        return '刚刚';
      }
      return minutes + '分钟前';
    }
  },

  //MM-dd hh:mm
  monthDayHourMinutes(date){
    if(date){
      var oDate = new Date(date);
      var month = (oDate.getMonth()+1) > 9 ? (oDate.getMonth()+1) : '0'+(oDate.getMonth()+1);// 获取月，
      var day = oDate.getDate() > 9 ? oDate.getDate() : '0' + oDate.getDate(); // 获取日，
      var hours = oDate.getHours() > 9 ? oDate.getHours() : '0' + oDate.getHours(); //获取时，
      var minutes = oDate.getMinutes() > 9 ? oDate.getMinutes() : '0' + oDate.getMinutes(); //分
      return month + '-' + day + ' ' + hours + ':' + minutes
    }else{
      return "无";
    }
  },

  //返回当年(yyyy-MM-dd hh:mm) 不是当年(YYYY-MM)
  convertYearOrHour(date){
    if(date){
      //创建时间
      var oDate = new Date(date.replace(new RegExp(/-/gm) ,"/"));
      var year = oDate.getFullYear();
      var month = (oDate.getMonth()+1) > 9 ? (oDate.getMonth()+1) : '0'+(oDate.getMonth()+1);// 获取月，
      var day = oDate.getDate() > 9 ? oDate.getDate() : '0' + oDate.getDate(); // 获取日，
      var hours = oDate.getHours() > 9 ? oDate.getHours() : '0' + oDate.getHours(); //获取时，
      var minutes = oDate.getMinutes() > 9 ? oDate.getMinutes() : '0' + oDate.getMinutes(); //分


      return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes;

      //当前系统时间
      // var currentDate = new Date();
      // var currentYear = currentDate.getFullYear();
      // if(year == currentYear){
      //   return year + '年' + month + '月' + day + '日 ' + hours + ':' + minutes;
      // }else if(year < currentYear){
      //   return year + '年' + month + '月';
      // }
    }else{
      return "无";
    }
  },

  //返回yyyy-mm-dd
  yearAndMonthAndDay(date){
    const oDate = new Date(date.replace(new RegExp(/-/gm) ,"/"));
    if(oDate){
      //创建时间
      var year = oDate.getFullYear();
      var month = (oDate.getMonth()+1) > 9 ? (oDate.getMonth()+1) : '0'+(oDate.getMonth()+1);// 获取月，
      var day = oDate.getDate() > 9 ? oDate.getDate() : '0' + oDate.getDate(); // 获取日，
      var hours = oDate.getHours() > 9 ? oDate.getHours() : '0' + oDate.getHours(); //获取时，
      var minutes = oDate.getMinutes() > 9 ? oDate.getMinutes() : '0' + oDate.getMinutes(); //分

      return year + '-' + month + '-' + day;
    }else{
      return "无";
    }
  },

  //转换显示格式
  convertDateTimeToCustom(dateValue){

    let time = 0;
    if(dateValue.indexOf('-') > 0){
      time = Date.parse(dateValue.replace(new RegExp(/-/gm), "/"));
    }else{
      time = Date.parse(dateValue);
    }
    //存储转换值
    var s;
    if(time<60*10){//十分钟内
        return '刚刚';
    }else if((time<60*60)&&(time>=60*10)){
        //超过十分钟少于1小时
        s = Math.floor(time/60);
        return  s+"分钟前";
    }else if((time<60*60*24)&&(time>=60*60)){
        //超过1小时少于24小时
        s = Math.floor(time/60/60);
        return  s+"小时前";
    }else if((time<60*60*24*3)&&(time>=60*60*24)){
        //超过1天少于3天内
        s = Math.floor(time/60/60/24);
        return s+"天前";
    }else{
        //超过3天
        var newDate= new Date(time);
        // console.log('newDate',newDate);
        var month = newDate.getMonth()+1;
        var day = newDate.getDate();
        if(month<10){
          month = '0'+month;
        }
        if(day<10){
          day = '0'+day;
        }
        return newDate.getFullYear()+"-"+month+"-"+day;
    }
  }
}
